package net.googlecode.garzha.controllers;

import net.googlecode.garzha.domains.Comment;
import net.googlecode.garzha.domains.Group;
import net.googlecode.garzha.domains.Person;
import net.googlecode.garzha.services.PersonService;
import net.googlecode.garzha.services.SecurityService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;


@Controller
public class HomeController {

    @Autowired
    private PersonService personService;
    @Autowired
    private SecurityService securityService;

    protected static final Logger logger = Logger.getLogger(IndexController.class);

    /**
     * Controller of home page.
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/home")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        ModelAndView mv = new ModelAndView("home");
        logger.info("Get current person");
        Person prs1 = personService.getPerson(securityService.getCurrentUser());
        Date d = new Date();


        logger.info("date  " + d.getDay());
        /**
         * Check the dates for the calculation of balls in order to vote
         */
        if (d.getDay() != prs1.getDay()) {
            prs1.setDay(d.getDay());
            prs1.setEnrate(5);
            personService.updatePerson(prs1);
        }

        logger.info("Set parametres that send to view");
        Person prs = personService.getPerson(securityService.getCurrentUser());
        logger.info("get group");
        Group gr = personService.getGroup(prs.getGrpid());


        logger.info("1list "+prs.getId().toString());

        /**
         * Getting list groupmate.
         */
        mv.addObject("groupmates", personService.getGroupMates(prs));
        mv.addObject("info", prs);
        mv.addObject("infogr", gr.getName());
        mv.addObject("root", request.getContextPath());
        mv.addObject("top5", personService.getTop5());
        mv.addObject("com",personService.getListComment(prs.getId().toString()));
        logger.info("Returning user page view");
        return mv;
    }
   @RequestMapping(value = "/addcomment", method = RequestMethod.POST)
    public String setPas(@RequestParam(value = "comment", required = true) String comment

                       ) {
        logger.info("pas add function ");
        Person prs = personService.getPerson(securityService.getCurrentUser());
        Comment com = new Comment();
       com.setComment(prs.getFname()+prs.getSname()+": "+comment );
       com.setPersonid(prs.getId().toString());
       com.setGuestid(prs.getId().toString());

        personService.addComment(com);
        logger.info("set return function ");
        return "redirect:/home";
    }
}